package com.itheima.Advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
//@Aspect
public class DivAdvice {
    @Pointcut("execution(* com.itheima.dao.BookDao.div(..))")
    public void pt() {
    }


    @Before("pt()")
    public void before(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        System.out.println("除法参数为=" + args[0] + "和" + args[1]);
        System.out.println("前置通知");
    }

    @After("pt()")
    public void after(JoinPoint joinPoint) {
        System.out.println("后置通知");
    }

    @AfterReturning(value = "pt()", returning = "r")
    public void method(JoinPoint joinPoint, Object r) {
        System.out.println("返回通知结果=" + r);
        System.out.println(joinPoint.getSignature().getName());
    }

    @AfterThrowing(value = "pt()", throwing = "t")
    public void afterthrowing(JoinPoint joinPoint, Throwable t) {
        System.out.println("抛出异常后通知" + t.getMessage());
    }

    @Around("pt()")
    public Object around(ProceedingJoinPoint pjp) {
        Object ret = null;
        System.out.println("环绕前置通知");
        try {
            Integer[] args = {8, 2};
            ret = pjp.proceed(args);
            System.out.println("环绕返回通知" + ret);
        } catch (Throwable e) {
            // e.printStackTrace();
            System.out.println("环绕异常通知" + e.getMessage());
        }
        System.out.println("环绕后置通知");
        return 0;
        //return ret=0;
    }

}
